package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.config.ServerConfig;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.ecard.config.domain.EcardNewsContentList;
import com.xydtech.ecard.config.domain.NewsContentRequest;
import com.xydtech.ecard.config.service.IEcardNewsContentListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 电子名片新闻列Controller
 *
 * @author JHON
 * @date 2022-04-11
 */
@Controller
@RequestMapping("/config/contentList")
public class EcardNewsContentListController extends BaseController {
    private String prefix = "config/contentList";

    @Autowired
    private IEcardNewsContentListService ecardNewsContentListService;
    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("config:contentList:view")
    @GetMapping()
    public String contentList() {
        return prefix + "/contentList";
    }

    /**
     * 查询电子名片新闻列列表
     */
    @RequiresPermissions("config:contentList:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardNewsContentList ecardNewsContentList) {
        startPage();
        List<EcardNewsContentList> list = ecardNewsContentListService.selectEcardNewsContentListList(ecardNewsContentList);
        return getDataTable(list);
    }

    /**
     * 下载电子名片新闻
     */
    @GetMapping("/down")
    public String down() {
        return prefix + "/down";
    }

    /**
     * 电子名片新闻下载
     */
    @RequiresPermissions("config:contentList:downLoad")
    @Log(title = "电子名片新闻下载", businessType = BusinessType.IMPORT)
    @PostMapping("/downLoad")
    @ResponseBody
    public AjaxResult downLoad(NewsContentRequest newsContentRequest) {
        newsContentRequest.setDeptId(ShiroUtils.getSysUser().getDeptId());
        return ecardNewsContentListService.downLoadNewsContentList(newsContentRequest);
    }

    /**
     * 新增电子名片新闻列
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片新闻列
     */
    @RequiresPermissions("config:contentList:add")
    @Log(title = "电子名片新闻列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardNewsContentList ecardNewsContentList, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardNewsContentList.setHtml(serverConfig.replaceContextPath(request, ecardNewsContentList.getHtml()));
        return toAjax(ecardNewsContentListService.insertEcardNewsContentList(ecardNewsContentList));
    }

    /**
     * 修改电子名片新闻列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, HttpServletRequest request, ModelMap mmap) {
        EcardNewsContentList ecardNewsContentList = ecardNewsContentListService.selectEcardNewsContentListById(id);
        String contextPath = request.getServletContext().getContextPath();
        if (contextPath.length() > 1 && ecardNewsContentList.getHtml() != null) {
            ecardNewsContentList.setHtml(ecardNewsContentList.getHtml().replaceAll("/profile", contextPath + "/profile"));
        }
        mmap.put("ecardNewsContentList", ecardNewsContentList);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片新闻列
     */
    @RequiresPermissions("config:contentList:edit")
    @Log(title = "电子名片新闻列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardNewsContentList ecardNewsContentList, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardNewsContentList.setHtml(serverConfig.replaceContextPath(request, ecardNewsContentList.getHtml()));
        return toAjax(ecardNewsContentListService.updateEcardNewsContentList(ecardNewsContentList));
    }

    /**
     * 修改电子名片新状态
     */
    @RequiresPermissions("config:contentList:edit")
    @Log(title = "修改电子名片新状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(EcardNewsContentList ecardNewsContentList, HttpServletRequest request) {

        return toAjax(ecardNewsContentListService.updateEcardNewsContentList(ecardNewsContentList));
    }

    /**
     * 删除电子名片新闻列
     */
    @RequiresPermissions("config:contentList:remove")
    @Log(title = "电子名片新闻列", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardNewsContentListService.deleteEcardNewsContentListByIds(ids));
    }
}
